package com.cmbchina.vat.core.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
/**
 * 
 * @ClassName: VatUtils.java
 * @Description: 时间
 *
 * @version: v1.0.0
 * @author: **
 * @date: 下午4:43:18
 *
 * Modification History:
 * Date         Author          Version            Description
 *---------------------------------------------------------*
 * 2018年4月5日     **           v1.0.0               修改原因
 */
public class VatUtils {
	private VatUtils(){}
	
	private static final String TIME_ZONE = "Asia/Shanghai";
	private static final String FORMAT_DATE8 = "yyyyMMdd";
	private static final String FORMAT_DATE10 = "yyyy-MM-dd";
	private static final String FORMAT_DATE14 = "yyyyMMddHHmmss";
	private static final String FORMAT_DATE17 = "yyyyMMddHHmmssSSS";
	private static final String FORMAT_DATE19 = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 获取当前yyyyMMdd时间
	 * @return
	 */
	public static String getCurrentDate8() {
		TimeZone defaultTimezone = TimeZone.getDefault();
		 // 时区转换
		Date date = new Date();
		Date dateTmp = null;
		int timeOffset = defaultTimezone.getRawOffset()-TimeZone.getTimeZone(TIME_ZONE).getRawOffset();
		dateTmp = new Date(date.getTime()-timeOffset);
		return DateFormatUtils.format(dateTmp, FORMAT_DATE8);
	}
	
	/**
	 * 获取当前时间前yearNumber年的yyyyMMdd时间
	 * @param range
	 * @return
	 */
	public static String getPreDate8(int yearNumber) {
		TimeZone defaultTimezone = TimeZone.getDefault();
		 // 时区转换
		Date date = new Date();
		Date dateTmp = null;
		int timeOffset = defaultTimezone.getRawOffset()-TimeZone.getTimeZone(TIME_ZONE).getRawOffset();
		dateTmp = new Date(date.getTime()-timeOffset);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTmp);
		calendar.add(Calendar.DAY_OF_YEAR, yearNumber);
		return DateFormatUtils.format(calendar.getTime(), FORMAT_DATE8);
	}
	
	/**
	 * 获取当前yyyy-MM-dd时间
	 * @return
	 */
	public static String getCurrentDate10() {
		TimeZone defaultTimezone = TimeZone.getDefault();
		 // 时区转换
		Date date = new Date();
		Date dateTmp = null;
		int timeOffset = defaultTimezone.getRawOffset()-TimeZone.getTimeZone(TIME_ZONE).getRawOffset();
		dateTmp = new Date(date.getTime()-timeOffset);
		return DateFormatUtils.format(dateTmp, FORMAT_DATE10);
	}
	
	/**
	 * 获取当前时间前monthNumber月yyyy-MM-dd
	 * @param monthNumber
	 * @return
	 */
	public static String getPreMonthDate10(int monthNumber){
		
		TimeZone defaultTimezone = TimeZone.getDefault();
		 // 时区转换
		Date date = new Date();
		Date dateTmp = null;
		int timeOffset = defaultTimezone.getRawOffset()-TimeZone.getTimeZone(TIME_ZONE).getRawOffset();
		dateTmp = new Date(date.getTime()-timeOffset);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTmp);
		calendar.add(Calendar.MONTH, monthNumber);
		return DateFormatUtils.format(calendar.getTime(), FORMAT_DATE10);
	}
	
	/**
	 *  获取当前yyyyMMddHHmmss时间
	 * @return
	 */
	public static String getCurrentDateTime14() {
		TimeZone defaultTimezone = TimeZone.getDefault();
		 // 时区转换
		Date date = new Date();
		Date dateTmp = null;
		int timeOffset = defaultTimezone.getRawOffset()-TimeZone.getTimeZone(TIME_ZONE).getRawOffset();
		dateTmp = new Date(date.getTime()-timeOffset);
		return DateFormatUtils.format(dateTmp, FORMAT_DATE14);
	}
	/**
	 * 获取当前yyyyMMddHHmmssSSS时间
	 * @return
	 */
	public static String getCurrentDateTime17() {
		TimeZone defaultTimezone = TimeZone.getDefault();
		 // 时区转换
		Date date = new Date();
		Date dateTmp = null;
		int timeOffset = defaultTimezone.getRawOffset()-TimeZone.getTimeZone(TIME_ZONE).getRawOffset();
		dateTmp = new Date(date.getTime()-timeOffset);
		return DateFormatUtils.format(dateTmp, FORMAT_DATE17);
	}
	/**
	 * 获取当前yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getCurrentDateTime19() {
		TimeZone defaultTimezone = TimeZone.getDefault();
		 // 时区转换
		Date date = new Date();
		Date dateTmp = null;
		int timeOffset = defaultTimezone.getRawOffset()-TimeZone.getTimeZone(TIME_ZONE).getRawOffset();
		dateTmp = new Date(date.getTime()-timeOffset);
		return DateFormatUtils.format(dateTmp, FORMAT_DATE19);
	}
	
	/**
	 * 获取UUID
	 * @return
	 */
	public static String getUUID(){
		return StringUtils.remove(UUID.randomUUID().toString(), "-");
	}
	
	/**
	 * 转换时区
	 * @param dateTime
	 * @param oldZone
	 * @param newZone
	 * @return
	 */
	public static String changeTimeZone(String dateTime,TimeZone oldZone,TimeZone newZone){
		if(StringUtils.isNotBlank(dateTime)){
			 // 时区转换
			Date date;
			try {
				date = DateUtils.parseDate(dateTime, new String[]{FORMAT_DATE19});
			} catch (ParseException e) {
				return dateTime;
			}
			Date dateTmp = null;
			int timeOffset = oldZone.getRawOffset()-newZone.getRawOffset();
			dateTmp = new Date(date.getTime()-timeOffset);
			return DateFormatUtils.format(dateTmp, FORMAT_DATE19);
		}else{
			return "";
		}
	}
	
	public static void main(String[] args) {
		System.out.println(VatUtils.getCurrentDate10());
		System.out.println(VatUtils.getPreMonthDate10(-1));
		
	}
	
}
